package com.woniu.controller;


import com.woniu.been.dto.CourseDTO;
import com.woniu.been.entity.Courses;
import com.woniu.been.entity.vo.MyClassVO;
import com.woniu.service.impl.CourseService;
import com.woniu.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class CoursesController {

    @Autowired
    CourseService courseService;
    /**
     * 增加课程观看次数
     */
    @GetMapping("add")
    public void addViewCount(Long courseId) {
        courseService.viewCourse(courseId);

    }

    /**
     * 获取热门课程列表
     * @param limit 排行榜的个数
     * @return
     */
    @RequestMapping("/hot")
    public List<CourseDTO> getHotCourses(@RequestParam(defaultValue = "3") int limit) {
        List<Courses> hotCourses = courseService.getHotCourses(limit);

        // 转换为DTO
        return hotCourses.stream()
                .map(course -> {
                    CourseDTO dto = new CourseDTO();
                    dto.setCourseId(course.getCourseId());
                    dto.setCourseName(course.getCourseName());
                    dto.setViewCount(course.getViewCount()); //course.getCourseId()
                    dto.setDescription(course.getDescription());
                    dto.setImgsrc(course.getImgsrc());
                    // 设置其他字段...
                    return dto;
                })
                .collect(Collectors.toList());
    }

    /**
     * 再‘我的课程’列表中获取课程详情
     * @return cxz
     */
    @RequestMapping("/myCourse")
    public ResponseResult findMyCourseById(@RequestParam Integer userId) {
        ;List<MyClassVO> myClass = courseService.findMyCourseById(userId);
        return ResponseResult.success(myClass);
    }


    /**
     * 通过用户id 查询拥有的课程id
     * @param userId 课程id
     * @return 用户拥有的课程id
     */
    @GetMapping("getMyCourseId")
    public ResponseResult getMyCourseId(@RequestParam Integer userId) {
        List<Integer> myCourseId = courseService.getMyCourseId(userId);
        return ResponseResult.success(myCourseId);
    }
    //根据课程id 查到对应的课程
}

